package com.central.apps.handler.po;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 采购单
 */
@Component
public class poHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        //采购单行，弹出框
        if("getPrline".equals(type)){
            String prnum= MapUtil.getStr(params,"prnum");
            String sql="select * from bl_demandapplyline where demandnum='"+prnum+"'";
            return BLuntil.toPage(params,handlerService,sql);

        }

        return super.operate(app, type, params);
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        // 保存后更新 总金额
        String ponum=MapUtil.getStr(data,"cgpaynum");
        List<Map<String, Object>> linelist = handlerService.findBySql("select ifnull(sum(cost),0) cost from bl_cgdanitem  where cgpaynum='"+ponum+"' ");
        if(linelist!=null && linelist.size()>0){
            String  cost =MapUtil.getStr(linelist.get(0),"cost");
            handlerService.findBySql("update bl_cgdan set actamount='"+cost+"' where cgpaynum ='"+ponum+"'");
        }

        super.afterSave(app, objName, data, isUpdate);
    }
}
